package com.alibaba.itu.tala.event;

import org.eclipse.compare.CompareEditorInput;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IFileEditorInput;
import org.eclipse.ui.ISelectionListener;
import org.eclipse.ui.IWorkbenchPart;

import com.alibaba.itu.tala.file.FileResource;

/**
 * Provides the listener to listen to the selection change.
 */
public class ReviewSelectionListener implements ISelectionListener {

    /** The selection. */
    private static IStructuredSelection structuredSelection;

    /**
     * Gets structured selection.
     * 
     * @return the structured selection.
     */
    public static IStructuredSelection getStructuredSelection() {
        return structuredSelection;
    }

    /**
     * Notified when selection is changed. Brings the opened code review editor
     * to top if a code review file is selected and the editor associated with
     * the file is opened.
     * 
     * @param workbenchPart the workbench part containing the selection
     * @param selection the current selection
     */
    public void selectionChanged(IWorkbenchPart workbenchPart, ISelection selection) {
        IEditorPart editorPart = workbenchPart.getSite().getPage().getActiveEditor();
        if (editorPart != null && editorPart.getEditorInput() instanceof IFileEditorInput) {
            IFile activeFile = ((IFileEditorInput) editorPart.getEditorInput()).getFile();
            FileResource.setSelectedResource(activeFile);
        } else if(editorPart != null && editorPart.getEditorInput() instanceof CompareEditorInput){
            IFile targetFile = FileResource.getFileInCompareEditor((CompareEditorInput)editorPart.getEditorInput());
            if(targetFile != null){
                FileResource.setSelectedResource(targetFile);
                return;
            }
            FileResource.setSelectedResource(null);
        } 
        if (selection instanceof IStructuredSelection) {
            structuredSelection = (IStructuredSelection) selection;
            if (structuredSelection.isEmpty()) {
                // clear out selection information
                FileResource.setSelectedResource(null);
            } else {
                Object object = structuredSelection.getFirstElement();
                if (object instanceof IAdaptable) {
                    IAdaptable adaptable = (IAdaptable) object;
                    IResource resource = (IResource) adaptable.getAdapter(IResource.class);
                    FileResource.setSelectedResource(resource);
                }
            }
        } else {
            FileResource.setSelectedResource(null);
        }
    }
}
